﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Office.Interop.UccApi;

namespace shfa.snomtastic.systray.components
{
    public partial class uccInfo : _IUccPlatformEvents,
                                    _IUccEndpointEvents,
                                    _IUccCategoryContextEvents,
                                    _IUccCategoryInstanceEvents,
                                    _IUccPresentityEvents
    {

        private IUccPlatform platform = null;
        private IUccEndpoint endpoint = null;

        private IUccSubscriptionManager subMgr;
        //private IUccPublicationManager pubMgr;

        private UccSubscription selfSubscription;
        //private UccSubscription contactsSubscription;

        private UccPresentity selfPresentity;

        public string SignName { get; set; }
        public string Password { get; set; }
        public string Domain { get; set; }
        public string ServerName { get; set; }
        public string Transport { get; set; }
        public string UriName { get; set; }
       

        public PresenceStateInfo Availability = new PresenceStateInfo();

        UccUriManager uriManager = new UccUriManager();

        public uccInfo(string signName, string password, string domain, string serverName, string transport, string uriName)
        {
            this.SignName = signName;
            this.Password = password;
            this.Domain = domain;
            this.ServerName = serverName;
            this.Transport = transport;
            this.UriName = uriName;

            CreatePlatform();
            CreateEndpoint();
            EnableEndpoint();
        }

        #region Intialise

        public void CreatePlatform()
        {

            this.platform = new UccPlatformClass();
            if (this.platform != null)
            {
                // Register this client with the platform to receive
                // notifications of the platform events
                Advise<_IUccPlatformEvents>(this.platform, this);

                // Initialize the platform
                this.platform.Initialize("Snomtastic Client", null);
                //IUccTraceSettings traceSettings = (IUccTraceSettings)this.platform;
                //traceSettings.EnableTracing();
            }
        }

        public void CreateEndpoint()
        {

            // Create endpoint

            this.endpoint = this.platform.CreateEndpoint(UCC_ENDPOINT_TYPE.UCCET_PRINCIPAL_SERVER_BASED, uriManager.ParseUri(UriName), null, null);

            // Configure the endpoint
            IUccServerSignalingSettings settings = (IUccServerSignalingSettings)this.endpoint;

            // Add the credentials -- note: "*" means any realm
            UccCredential credential = settings.CredentialCache.DefaultCredential;
            // UccCredential credential = settings.CredentialCache.CreateCredential("jamie.barnett@shfa.nsw.gov.au", "snowcrash#12", "SYDROCK");
            settings.CredentialCache.SetCredential("*", credential);

            // Set the server to use
            settings.Server = settings.CreateSignalingServer(ServerName, (Transport == "TCP") ? UCC_TRANSPORT_MODE.UCCTM_TCP : UCC_TRANSPORT_MODE.UCCTM_TLS);


            // Set the allowed authentication modes
            settings.AllowedAuthenticationModes = (int)UCC_AUTHENTICATION_MODES.UCCAM_KERBEROS
                                                    | (int)UCC_AUTHENTICATION_MODES.UCCAM_NTLM;

            // Register this client to receive event
            // notifications when the login session changes.
            Advise<_IUccEndpointEvents>(this.endpoint, this);
            Advise<_IUccSessionManagerEvents>(this.endpoint, this);
        }

        public void EnableEndpoint()
        {
            if (endpoint != null)
            {
                this.endpoint.Enable(null);
            }
        }

        public void DisableEndpoint()
        {
            if (this.endpoint != null)
            {
                this.endpoint.Disable(null);
            }
        }

        #endregion

        #region Helpers

        private void SelfSubscribe()
        {
            try
            {
                this.subMgr = (IUccSubscriptionManager)endpoint;

                this.selfSubscription = this.subMgr.CreateSubscription(null);
                this.selfPresentity = this.selfSubscription.CreatePresentity(uriManager.ParseUri(UriName), null);
                Advise<_IUccPresentityEvents>(this.selfPresentity, this);

                this.selfSubscription.AddPresentity(this.selfPresentity);
                this.selfSubscription.AddCategoryName("state");
                this.selfSubscription.Subscribe(null);

               
            }
            catch { }
        }

        static void Advise<T>(object source, T sink)
        {
            IConnectionPointContainer container = (IConnectionPointContainer)source;
            IConnectionPoint cp;
            int cookie;
            Guid guid = typeof(T).GUID;
            container.FindConnectionPoint(ref guid, out cp);
            cp.Advise(sink, out cookie);
        }

        #endregion

        #region _IUccPlatformEvents Members

        public void OnIpAddrChange(UccPlatform pEventSource, object pEventData) { }

        public void OnShutdown(UccPlatform pEventSource, IUccOperationProgressEvent pEventData)
        {
            if (pEventData.IsComplete)
            {
                if (pEventData.StatusCode >= 0)
                {
                    this.platform = null;
                }
            }
        }

        #endregion

        #region _IUccEndpointEvents Members

        void _IUccEndpointEvents.OnDisable(IUccEndpoint pEventSource, IUccOperationProgressEvent pEventData)
        {
            if (pEventData.IsComplete)
                if (pEventData.StatusCode >= 0)
                    this.endpoint = null;
        }

        void _IUccEndpointEvents.OnEnable(IUccEndpoint pEventSource, IUccOperationProgressEvent pEventData)
        {
            if (pEventData.IsComplete)
            {
                if (pEventData.StatusCode >= 0)
                {
                   
                    //this.pubMgr = (IUccPublicationManager)endpoint;
                    this.SelfSubscribe();
                }
                else
                {
                    this.endpoint = null;
                }
            }
        }

        #endregion

        #region _IUccCategoryContextEvents Members

        void _IUccCategoryContextEvents.OnCategoryInstanceAdded(IUccCategoryContext pCategoryCtxt, UccCategoryInstanceEvent pCategoryInstanceEvent)
        {

            Advise<_IUccCategoryInstanceEvents>(pCategoryInstanceEvent.CategoryInstance, this);

        }

        void _IUccCategoryContextEvents.OnCategoryInstanceRemoved(IUccCategoryContext pCategoryCtxt, UccCategoryInstanceEvent pCategoryInstanceEvent) { }

        #endregion

        #region _IUccCategoryInstanceEvents Members

        void _IUccCategoryInstanceEvents.OnCategoryInstanceValueModified(IUccCategoryInstance pCategoryInstance, object pEventData)
        {
            IUccPresenceStateInstance state = (IUccPresenceStateInstance)pCategoryInstance;
            this.Availability.SetState(state.Availability);
        }

        #endregion

        #region _IUccPresentityEvents Members

        void _IUccPresentityEvents.OnCategoryContextAdded(UccPresentity pPresentity, UccCategoryContextEvent pCategoryCtxtEvent)
        {
            Advise<_IUccCategoryContextEvents>(pCategoryCtxtEvent.CategoryContext, this);
        }

        void _IUccPresentityEvents.OnCategoryContextRemoved(UccPresentity pPresentity, UccCategoryContextEvent pCategoryCtxtEvent) { }

        #endregion


      
    }
}
